/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DanhSachHocSinhInternalFrame.java
 *
 * Created on Jul 28, 2010, 2:47:57 AM
 */

package GUI;

import BUS.DiemHocTapBUS;
import BUS.HocSinh_LopBUS;
import BUS.NamHocBUS;
import POJO.HocSinh_LopPOJO;
import POJO.NamHocPOJO;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 * @author
 *  ITEC.HCMUS.EDU.VN
 *  Ngô Huy Đạt
 *  0869005
 *  nhdat.itec@gmail.com
 */
public class DanhSachHocSinhInternalFrame extends javax.swing.JInternalFrame {

    private ArrayList<String> DSMaNamHoc = new ArrayList<String>();

    public void loadjCBNamHoc(){
        try{
            ArrayList<NamHocPOJO> list = NamHocBUS.selectAll();
            DefaultComboBoxModel dcb = new DefaultComboBoxModel();
            int size = list.size();
            for(int i=0;i<size;i++){
                this.DSMaNamHoc.add(list.get(i).getMaNamHoc());
                dcb.addElement(list.get(i).getNamHoc());
            }
            this.jCBNamHoc.setModel(dcb);
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public void showList(){
        try{
            String MaNH = this.DSMaNamHoc.get(this.jCBNamHoc.getSelectedIndex());
            ArrayList<HocSinh_LopPOJO> list = HocSinh_LopBUS.selectHocSinh_Nam(MaNH);
            DefaultTableModel dtm = new DefaultTableModel(){
                @Override
                public boolean isCellEditable(int row,int column){
                return false;
                }
            };
            dtm.addColumn("STT");
            dtm.addColumn("Tên học sinh");
            dtm.addColumn("Tên lớp");
            dtm.addColumn("TB Học Kỳ 1");
            dtm.addColumn("TB Học Kỳ 2");
            int size = list.size();
            dtm.setRowCount(size);
            for(int i=0;i<size;i++){
                dtm.setValueAt(i+1, i, 0);
                dtm.setValueAt(list.get(i).getHocSinh(), i, 1);
                dtm.setValueAt(list.get(i).getLop(), i, 2);
                double hk1 = DiemHocTapBUS.averageHK(list.get(i).getMaHocSinh(), "1", MaNH);
                if(hk1==-1){
                    dtm.setValueAt("- -", i, 3);

                }else{
                    dtm.setValueAt(String.format("%.1f", hk1), i, 3);
                }
                double hk2 = DiemHocTapBUS.averageHK(list.get(i).getMaHocSinh(), "2", MaNH);
                if(hk2==-1){
                    dtm.setValueAt("- -", i, 4);
                }else{
                    dtm.setValueAt(String.format("%.1f", hk2), i, 4);
                }
            }
            this.jTDanhSach.setModel(dtm);
            this.jLSoHocSinh.setText(String.valueOf(this.jTDanhSach.getRowCount()));
        }catch(Exception ex){
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /** Creates new form DanhSachHocSinhInternalFrame */
    public DanhSachHocSinhInternalFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jBThoat = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTDanhSach = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLSoHocSinh = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jCBNamHoc = new javax.swing.JComboBox();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Quản lý học sinh - Danh sách học sinh");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jBThoat.setText("Thoát");
        jBThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBThoatActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabel1.setForeground(new java.awt.Color(0, 102, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Danh sách học sinh");

        jTDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTDanhSach);

        jLabel2.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel2.setText("Copyright ©2010 - Group 12™");
        jLabel2.setEnabled(false);

        jLabel3.setText("học sinh");

        jLSoHocSinh.setText("0");

        jLabel5.setText("Tổng số:");

        jLabel4.setText("Năm học:");

        jCBNamHoc.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBNamHocItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 182, Short.MAX_VALUE)
                .addComponent(jBThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(363, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLSoHocSinh)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(137, 137, 137)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(129, 129, 129))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jCBNamHoc, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLSoHocSinh)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jCBNamHoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jBThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        this.loadjCBNamHoc();
        this.showList();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jCBNamHocItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBNamHocItemStateChanged
        // TODO add your handling code here:
        this.showList();
    }//GEN-LAST:event_jCBNamHocItemStateChanged

    private void jBThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jBThoatActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBThoat;
    private javax.swing.JComboBox jCBNamHoc;
    private javax.swing.JLabel jLSoHocSinh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTDanhSach;
    // End of variables declaration//GEN-END:variables

}
